<?php

class Reale_Model_Estate_SearchMapper
{	
	/**
     * @var Zend_Db_Table_Abstract
     */
    protected $_dbTable;
    

    /**
     * Specify Zend_Db_Table instance to use for data operations
     * 
     * @param  Zend_Db_Table_Abstract $dbTable 
     * @return Reale_Model_Estate_SearchMapper
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Get registered Zend_Db_Table instance
     *
     * Lazy loads Reale_Model_DbTable_Estate if no instance registered
     * 
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Reale_Model_DbTable_Estate');
        }
        return $this->_dbTable;
    }
    
    public function search(Reale_Model_Estate_Search $search) {
        $select = $this->getDbTable()->getAdapter()
                        ->select()
                        ->from('estate',array('estate_id',
                        'bhk',
                        'status_id',
                        'count',
                        'rate',
                        'state',
                        'city',
                        'location',
                        'summary'))
                        ->join('type', 
                        		'(`estate`.`type_id` = `type`.`type_id`) AND (`estate`.`category_id` = `type`.`category_id`)',
                                array('type_name'))
                        ->join('project', '(`estate`.`estate_id` = `project`.`project_id`)',array('builder','project_name'));
                        
        
        $q = $search->getCategory_id();
        if (isset($q)) {
            $select->where('estate.category_id = ?',$search->getCategory_id());
        }
        $q = $search->getType_id();
        if (isset($q)) {
            $select->where('estate.type_id = ?',$search->getType_id());
        }
        $q = $search->getStatus_id();
        if (isset($q)) {
            $select->where('status_id = ?',$search->getStatus_id());
        }
        $q = $search->getCity();
        if (isset($q)) {
            $select->where('city = ?',$search->getCity());
        }
        $q = $search->getLocation();
        if (isset($q)) {
            $select->where('location = ?',$search->getLocation());
        }
        $q = $search->getBhk();
        if (isset($q)) {
            $select->where('bhk = ?',$search->getBhk());
        }
        $q = $search->getRate();
        if (isset($q)) {
            $select->where('rate = ?',$search->getRate());
        }
        $q = $search->getSize();
        if (isset($q)) {
            if (strstr($q, '-')) {
                $q = explode($q, '-');
                if (isset($q[1]) and $q[1]) {
                    $select->where("size between $q[0] and ?",$q[1]);
                } else {
                    $select->where("size > ?",$q[0]);
                }
            } else {
                $select->where('size = ?',$search->getSize());
            }
        }
        $q = $search->getState();
        
        if (isset($q)) {
            $select->where('state = ?',$search->getState());
        }
        
        return $select->query()->fetchAll();
    }
    
    /**
     * 
     * For retreiving the distinct cities from estate database.
     * @return array 
     */
    public function getCities($district = NULL){
    	
    	$select = $this->getDbTable()->getAdapter()
    				->select()
    				->distinct()
    				->from('estate',array('city'));
    	if (!is_null($district)) {
    	    $select->where('district = ?',$district);
    	}
    	return $select->query()->fetchAll();
    				
    }


    /**
     * 
     * For retreiving the distinct cities from estate database.
     * @return array 
     */
    public function getDistricts($state = NULL){
    	
    	$select = $this->getDbTable()->getAdapter()
    				->select()
    				->distinct()
    				->from('estate',array('district'));
    	if (!is_null($state)) {
    	    $select->where('state = ?',$state);
    	}
    	return $select->query()->fetchAll();
    				
    }
    
    /**
     * 
     * For retreiving the distinct states from estate database.
     * @return array 
     */
    public function getStates(){
    	
    	$select = $this->getDbTable()->getAdapter()
    				->select()
    				->distinct()
    				->from('estate',array('state'));
    	
    	return $select->query()->fetchAll();
    				
    }
}

